package parser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import parser.exceptions.InvalidTemplateException;

import leerxml.LeerXml;
import leerxml.TemplateStructure;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.EndTagType;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.MasonTagTypes;
import net.htmlparser.jericho.PHPTagTypes;
import net.htmlparser.jericho.Segment;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import net.htmlparser.jericho.StartTagType;
import net.htmlparser.jericho.StreamedSource;
import net.htmlparser.jericho.TextExtractor;

public class WikiParser {
	static List<Elemento> 	listaDeSecciones = new ArrayList<Elemento>();
	

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
//		String wikitext = "";
		
			URL url= new URL("https://wiki.sei.cmu.edu/sad/index.php/The_Java_Pet_Store_SAD");
		
		
//        URLConnection yc = url.openConnection();
//        BufferedReader in = new BufferedReader(new InputStreamReader(
//                                    yc.getInputStream()));
//		
//        String inputLine;
//        while ((inputLine = in.readLine()) != null) 
//        	wikitext+=inputLine;
//        in.close();
//        
//        System.out.println(wikitext);
//        String html = Parser.toHtml(wikitext, null);
//		System.out.println(html);
		
		Source source=new Source(url);
		
//		source.getURIAttributes();
//		System.out.println("##################### PHP tag types now added to register #####################\n");
//
//		System.out.println("A:");
//		displaySegments(source.getAllElements(HTMLElementName.A));
//		System.out.println("Views");
//		source.getChildElements();
//		List<StartTag> tags = source.getAllStartTags("p");
//		for (int i = 0; i < tags.size(); i++) {
//			StartTag tag = tags.get(i);
//			tag.getAllElements();
//			Segment content=  tag.getTagContent();
//			 TextExtractor tExtractor= content.getTextExtractor();
//			 System.out.println(tExtractor.toString());
//		}
		String urlBase= url.getProtocol()+"://"+url.getHost();
		generateStructure(source,urlBase);
//		allElement(source,null);
//		StreamedSource streamedSource=new StreamedSource(url);
//	
//		for (Segment segment : streamedSource) {
//			System.out.println(segment.getDebugInfo()+"\n");
//			
//		}
//		System.out.println(source.getRenderer());
//	

	}

	private static void allElement(Segment source, Elemento elemento) {
		Elemento elementSad= null;		
		List<Element> elementList=source.getChildElements();
		
		for (Element element : elementList) {
			System.out.println("-------------------------------------------------------------------------------");
			System.out.println(element.getDebugInfo());
			if(!element.isEmpty()){
//				elementSad = new ElementoCompuesto(element);
				listaDeSecciones.add(elementSad);
				allElement(element,elementSad);					
				}else{
					if (element.getAttributes()!=null)
						System.out.println("XHTML StartTag:\n"+element.getStartTag().tidy(false));
					System.out.println("Source text with content:\n"+element);
					elementSad = new ElementoSimple(element);
					listaDeSecciones.add(elementSad);}
					
				}		
			
		}
		
	
	private static void generateStructure(Source source, String urlBase) {
	
		List<TemplateStructure> templateFormat = new LeerXml().getStructure();
		Sad sad = new Sad(urlBase,templateFormat,source);	
//		System.out.println(source.getRenderer().toString());
		try {
			sad.generate();
		} catch (InvalidTemplateException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
				
			
		}
	
	private static void displaySegments(List<? extends Segment> segments) {
		for (Segment segment : segments) {
			System.out.println("-------------------------------------------------------------------------------");
			System.out.println(segment.getDebugInfo());
			System.out.println(segment);
		}
		System.out.println("\n*******************************************************************************\n");
	}

}
